package best.driver.android.common.model;

import java.util.ArrayList;
import java.util.WeakHashMap;

public class AbstractModel implements IModel {

    private WeakHashMap<ModelListener, Void> modelListeners = new WeakHashMap<ModelListener, Void>();

    public void addListener(ModelListener listener) {
        modelListeners.put(listener, null);
    }

    public void removeListener(ModelListener listener) {
        modelListeners.remove(listener);
    }

    public void removeAllListeners() {
        modelListeners.clear();
    }

    @Override
    public boolean isDataLoaded() {
        return false;
    }

    @Override
    public boolean isLoadingData() {
        return false;
    }

    @Override
    public boolean isLoadingMoreData() {
        return false;
    }

    @Override
    public boolean isDataOutdated() {
        return false;
    }

    @Override
    public void loadData() {

    }

    @Override
    public void cancelLoad() {

    }

    protected void notifyModelStartLoad() {
        ArrayList<ModelListener> listeners = new ArrayList<ModelListener>(modelListeners.keySet());
        for (ModelListener aListener : listeners) {
            aListener.onModelDidStartLoad(this);
        }
    }

    protected void notifyModelFinishLoad() {
        ArrayList<ModelListener> listeners = new ArrayList<ModelListener>(modelListeners.keySet());
        for (ModelListener aListener : listeners) {
            aListener.onModelDidFinishLoad(this);
        }
    }

    protected void notifyModelLoadFailed(Exception error) {
        ArrayList<ModelListener> listeners = new ArrayList<ModelListener>(modelListeners.keySet());
        for (ModelListener aListener : listeners) {
            aListener.onModelDidFailed(this, error);
        }
    }
}
